import WS from './ws'
import app from '../../assets/js/app'
const wsUrl = 'webSocketUrl' // 可改为真实的websocket 接口
let wsConnection
// 成功之后的数据处理 -可根据具体 数据格式进行处理
const handleData = (v) => {
  if (v.code != 200) {
    wsConnection.closeConnect()
    wsConnection = null
    return
  }
  if (!wsConnection) {
    wsConnection = true
    app.$emit(app.EventType.dataInit, v.data)
  }
  if (wsConnection) {
    app.dataUpdate('/firstPage', 'leftCenterTop.chartData[0].name', v.data)
  }
}

const init = () => {
  if (wsConnection) return
  wsConnection = new WS({
    url: wsUrl,
    successHandler: (v) => {
      handleData(v)
    },
    errorHandler: () => {
      console.log('ws_Error')
    }
  })
  app.log('websocket init')
}

export { init }
